<?php

namespace Tbf\Bundle\UploadBundle;

use Tbf\Component\FileSystem\FileSystemFunction;

class MemoryBlob implements BlobInterface
{
    protected $content;
    protected $extension;
    public static function fromFile($file_path,$extension){
        return new MemoryBlob(file_get_contents($file_path),$extension);
    }
    public function __construct($content,$extension)
    {
        $this->content = $content;
        $this->extension = $extension;
    }
    /**
     * 类型,扩展名
     * @return string
     */
    public function getExtension()
    {
        return $this->extension;
    }

    /**
     * 大小
     * @return int
     * @return error
     */
    public function getSize()
    {
        return array(strlen($this->content),null);
    }

    /**
     * 全部内容,一次读取
     * @return string
     * @return error
     */
    public function getContent()
    {
        return array($this->content,null);
    }

    /**
     * 写入全部内容,一次写入
     * @param $string
     * @return error
     */
    public function setContent($string)
    {
        $this->content = $string;

        return null;
    }

    /**
     * 内容的md5值
     * @return string
     * @return error
     */
    public function getContentMd5()
    {
        return md5($this->content);
    }

    /**
     * 保存到某个文件中
     * @param string $file_path
     * @return string|null error
     */
    public function saveToFilePath($file_path)
    {
        FileSystemFunction::putContent($file_path,$this->content);
    }

}
